package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.BusinessException;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.usercard.PatientCardAddReqDTO;
import com.yixin.hospital.entity.vo.PatientCardVo;
import com.yixin.hospital.service.PatientCardService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/patient-card")
@Api(value = "缴费", tags = "缴费")
@Slf4j
public class PatientCardController {

    @Autowired
    private PatientCardService patientCardService;


    @GetMapping("/list")
    @ApiOperation(value = "会员卡列表")
    public ApiResult<List<PatientCardVo>> list(@RequestParam Long patientId) {
        return patientCardService.allList(patientId);
    }

    @GetMapping("/allStoredCardList")
    @ApiOperation(value = "所有会员储值卡列表")
    public ApiResult<List<PatientCardVo>> allStoredCardList() {
        return patientCardService.allStoredCardList();
    }

    @PostMapping("/add")
    @ApiOperation(value = "办卡")
    @Transactional(rollbackFor = BusinessException.class)
    public ApiResult add(@Valid @RequestBody PatientCardAddReqDTO userCardAddReqDto, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return patientCardService.add(userCardAddReqDto, userName);
    }

}
